package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //使用jedisPool
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/upload")
    public Result setmealController(@RequestParam("imgFile") MultipartFile imgFile){
        //这个是获得原始的文件名
        String originalFilename = imgFile.getOriginalFilename();
        //这个是为了截取到最后的后缀名eg:.jpg
        int index = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(index);
        //这个是为了让他图片名字不重复,所以用uuid
        String s = UUID.randomUUID().toString() + substring;
        try {
            //将文件上传到七牛
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),s);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,s);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,s);
    }

    @RequestMapping("/add")
    public Result add(Integer[]checkgroupIds, @RequestBody Setmeal setmeal){
        try{
            setmealService.add(checkgroupIds,setmeal);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.pageQuery(queryPageBean);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            setmealService.delete(id);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return  new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Setmeal setmeal=setmealService.findById(id);
            return  new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,setmeal);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Setmeal> list=setmealService.findAll();
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try{
            List<Integer> list=setmealService.findCheckItemIdsByCheckGroupId(id);
            System.out.println(list);
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[]checkgroupIds,@RequestBody Setmeal setmeal){
        try{
           setmealService.edit(checkgroupIds,setmeal);
            return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
